'use strict';
/* Controllers */
define(["app"], function(app){
	/** 用户信息 **/
	var MessageListCtrl = ['$state','$scope', '$http','$stateParams', function($state, $scope, $http,$stateParams){
		$scope.friend_messages = [];    //好友动态
		$scope.my_messages = [];    //我的说说
		$scope.about_messages = [];	   //与我相关
		

		//根据消息类型加载,type:栏目类型，msg_type：消息类型
		$scope.loadMessage = function(type,msg_type){
			// $scope.user 由父controller 传递而来
			//var params = {"type":type, "user_id": $scope.user.id,"msg_type":msg_type};   
			var params = {"type":type, "user_id": $stateParams.user_id,"msg_type":msg_type};  

			$http({	method:'post',
				url:"message/findByUserId",
				params:params
			}).success(function(result){
				var option = {page : result.datas, url:"message/findByUserId",params:params};
				
				var messages = $.handlePage(option);
				if(type == 1){
					$scope.my_messages = messages;
				}else if(type == 2){
					$scope.about_messages = messages;
				}else{
					$scope.friend_messages = messages;
				}
			}).error(function(){
				alert("网络连接失败");
			});
		};
		// 初始化空间动态
		$scope.loadMessage(0,1);
		
		// 发表 说说
		$scope.message = {};
		$scope.addMessage = function(type){
			var content = $("#state").text();
			if(!content){
				alert("请填写内容");return;
			}
			$scope.message.content = content;
			$scope.message.msg_type = type;
			$http({	method:'post',
				url:"message/addMessage",
				params:$scope.message
			}).success(function(result){
				if(result.status == 1){
					//$state.reload();   这个方法没用 , 不知道存在的价值 
					$state.go($state.$current, null, { reload: true });  // 重载当前页面
				}else{
					alert(result.msg);
				}
			});
		};
		
		//删除动态
		$scope.deleteMessage = function(message){
			if(!confirm("删除此条动态?"))return;
			
			var params = {msg_id:message.id};
			$http({	method:'post',
				url:"message/deleteMessage",
				params:params
			}).success(function(result){
				if(result.status == 1){
					$state.go($state.$current, null, { reload: true });  // 重载当前页面
				}else{
					alert(result.msg);
				}
			});
		};
		
		//-------------------------好友动态-----------------------------------------------------
		//显示 评论 窗口
		$scope.toggleComment = function(message){
			var collapse = "#collapse_"+message.id;
			var show =  $(collapse).bindSlide();
			if(show){
				var params = {type_id : message.id, type:2};
				$http({	method:'post',
					url:"comment/findByTypeId",
					params:params
				}).success(function(result){
					if(result.status == 1){
						message.comments = result.comments;
					}
				}).error(function(){
					alert("网络连接失败");
				});
			}
			
			
		};
		
		//添加评论
		$scope.comment = {};
		//添加评论，参数（message：评论对象，divId：编辑框id）
		$scope.addComment = function(message,divId){
			var content = $("#"+divId+message.id).text();
			if(!content){
				alert("请填写回复内容");return;
			}
			$scope.comment.type_id = message.id;
			$scope.comment.type = "2";    		// 消息评论
			$scope.comment.receiver_id = message.sender_id;
			$scope.comment.content = content;
			
			$http({	method:'post',
				url:"comment/addComment",
				params:$scope.comment
			}).success(function(result){
				//alert(result.msg);
				if(result.status == 1){
					$("#"+divId+message.id).text("");
					
					message.browse_num=message.browse_num+1;//浏览量+1
					message.comment_num=message.comment_num+1;//评论+1

					message.comments.push(result.comment);
				}
			}).error(function(){
				alert("网络连接失败");
			});
		};
		
		//好友动态--显示回复框
		$scope.toggleReply = function(comment,message){
			//alert("show"+comment.id);
			$("#friend_reply_"+comment.id).bindSlide();
			$("#msg"+message.id).bindSlide();
			
		};

		//---------------------------------我的说说----------------------------------------------------
		$scope.myComment=function(message){
			var collapse = "#my_"+message.id;
			var show =  $(collapse).bindSlide();
			if(show){
				var params = {type_id : message.id, type:2};
				$http({	method:'post',
					url:"comment/findByTypeId",
					params:params
				}).success(function(result){
					if(result.status == 1){
						message.comments = result.comments;
					}
				}).error(function(){
					alert("网络连接失败");
				});
			}
		}
		//我的说说---显示回复编辑框
		$scope.myReply=function(comment,message){
			$("#my_reply_"+comment.id).bindSlide();
			$("#myMsg"+message.id).bindSlide();
		};
		
		//---------------------------------与我相关----------------------------------------------------
		//与我相关--显示对话列表
		$scope.showDialogue=function(message){
			//alert(message.type_id);
			var collapse = "#dialogue_"+message.id;
			var show =  $(collapse).bindSlide();
			
			if(show){
				var params = {"type_id": message.type_id,type:3,"user_id": $scope.user.id};
				$http({	method:'post',
					url:"comment/findByTypeId",
					params:params
				}).success(function(result){
					if(result.status == 1){
						message.comments = result.comments;
					}
				}).error(function(){
					alert("网络连接失败");
				});
			}
			
		};
		//与我相关--显示回复编辑框
		$scope.replyDialog=function(comment,message){
			$("#about_reply_"+comment.id).bindSlide();
		};
		
		
		//---------------------------------------------------------------------------------
		//添加回复(参数：评论对象，当前用户id，编辑框id)
		$scope.addReply=function(message,comment,userId,divId){
			var content = $("#"+divId+comment.id).text();
			if(!content){
				alert("请填写回复内容");return;
			}
			$scope.comment.type_id = comment.type_id;
			$scope.comment.type = "2";    		// 消息评论
			$scope.comment.receiver_id = comment.sender_id;
			$scope.comment.content = content;
			
			$http({	method:'post',
				url:"comment/addComment",
				params:$scope.comment
			}).success(function(result){
				//alert(result.msg);
				if(result.status == 1){
					$("#"+divId+comment.id).text("");
					var reply=divId.substring(0,divId.indexOf("editable"));
					$("#"+reply+comment.id).bindSlide();

					message.browse_num=message.browse_num+1;//浏览量+1
					message.comment_num=message.comment_num+1;//评论+1
					message.comments.push(result.comment);
				}
			}).error(function(){
				alert("网络连接失败");
			});
			
			
		};
		//赞
		$scope.vote=function(message,userId){
			var ps={msg_id:message.id,user_id:userId};
			$http({	method:'post',
				url:"message/vote",
				params:ps
			}).success(function(result){
				if(result.status == 1){
					message.vote_num=message.vote_num+result.num;
					message.browse_num=message.browse_num+1;
					$("#a_vote"+message.id).unbind("click");
				}
			}).error(function(){
				alert("网络连接失败");
			});
		};
		//查看全文/浏览
		$scope.browse=function(){
			
		};
		$scope.user_messages =[];
		$scope.sys_messages = [];
		//查看留言板
		$scope.loadMsg=function(msg_type){
			var ps = {"type":20,"user_id":$stateParams.user_id,"msg_type":msg_type};  
			$http({	method:'post',
				url:"message/findByUserId",
				params:ps
			}).success(function(result){
				var option = {page : result.datas, url:"message/findByUserId",params:ps};
				var messages = $.handlePage(option);
				if(msg_type == 2){//用户留言
					$scope.user_messages = messages;
				}else if(msg_type == 3){//系统留言
					$scope.sys_messages = messages;
				}
			}).error(function(){
				alert("网络连接失败");
			});
		};
		$scope.loadMsg(2);
	}];
	
	return {messageList: MessageListCtrl};
});
